package com.tg.app;

import android.app.Application;
import android.content.Context;
import androidx.multidex.MultiDex;
import com.alibaba.fastjson.JSONObject;
import com.tg.appcommon.sdk.TGErrorCode;
import com.tg.app.helper.ActivityHelper;

public class TGSdk {
    public static final String KEY_UUID = "uuid";
    public static final String KEY_MSG = "msg";
    public static final String KEY_NAME = "name";
    public static TGSdk getInstance() {
        return SingletonHolder.sInstance;
    }
    private final TGSdkHelper mTGSdkHelper =  new TGSdkHelper();

    public boolean isInitSdk(){
        return mTGSdkHelper.isInitSdk();
    }
    public void initSdk(String appId, String token, final InitSdkListener listener){
        mTGSdkHelper.checkToken(appId, token, "", listener);
    }

    public String getAppId() {
        return mTGSdkHelper.getAppId();
    }

    public void showLoginActivity(Context context, TGDevice.TGDeviceListener listener){
        if (!TGSdk.getInstance().isInitSdk()){
            if (listener != null){
                listener.onResult(false, TGErrorCode.ERROR_NOT_INIT_SDK, "TGSdk::initSdk fail or no call");
            }
        }else{
            ActivityHelper.startLoginActivity(context, null);
        }
    }

    public String getToken(){
        return mTGSdkHelper.getToken();
    }

    @Deprecated
    public void notifyThirdPartyApp(int messageId, String messageInfo){
        mTGSdkHelper.notifyThirdPartyApp(messageId, messageInfo);
    }

    public void notifyMessage(int messageId, String message){
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put(TGSdk.KEY_MSG, message);
        }catch (Exception e){
            e.printStackTrace();
        }
        String param = jsonObject.toJSONString();
        TGSdk.getInstance().notifyThirdPartyApp(messageId, param);
    }


    public boolean isThirdPartyApp(){
        return mTGSdkHelper.isThirdPartyApp();
    }

    public void attachBaseContext(Context base){
        // you must install multiDex whatever tinker is installed!
        MultiDex.install(base);
    }

    public void initApp(Application application, TGNotifyListener listener, boolean log){
        mTGSdkHelper.initApp(application, listener, log);
    }

    public void onConfigurationChanged(Context context) {
        if (mTGSdkHelper != null) {
            mTGSdkHelper.onConfigurationChanged(context);
        }
    }

    private static class SingletonHolder {
        private static final TGSdk sInstance = new TGSdk();
    }

    public interface InitSdkListener {
        void onResult(boolean result, int errorCode, String errorInfo);
    }

    public interface TGNotifyListener{
        void onNotify(int messageId, String param);
    }



}
